<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="UTF-8" />
    <title>动态信封改进版</title>
    <style>
      body {
        background: #ee9ca7; /* fallback for old browsers */
        background: -webkit-linear-gradient(
          to right,
          #ffdde1,
          #ee9ca7
        ); /* Chrome 10-25, Safari 5.1-6 */
        background: linear-gradient(
          to right,
          #ffdde1,
          #ee9ca7
        ); /* W3C, IE 10+/ Edge, Firefox 16+, Chrome 26+, Opera 12+, Safari 7+ */
      }
      /* 新增信封浮动动画 */
      @keyframes float {
        0%,
        100% {
          transform: translateY(0);
        }
        50% {
          transform: translateY(-20px);
        }
      }

      .envelope-container {
        perspective: 1000px;
        position: relative;
        width: 100vw;
        height: 100vh;
        margin: 50px auto;
        cursor: pointer;
        display: flex;
        justify-content: center;
        align-items: center;
        transition: all 0.5s ease-in-out;
      }

      .envelope-icon {
        position: absolute;
        font-size: 200px;
        color: #c62828;
        transition: all 0.8s cubic-bezier(0.68, -0.55, 0.27, 1.55);
        transform-style: preserve-3d;
        animation: float 3s ease-in-out infinite;
        text-shadow: 0 4px 12px rgba(198, 40, 40, 0.3);
        transition: all 0.5s ease-in-out;
      }

      .letter-content {
        position: relative;
        width: 100%;
        height: 100%;
        padding: 40px;
        background: rgba(255, 255, 255, 0.9) url("./img/letterBg.jpg") no-repeat
          center/cover;
        box-shadow: 0 10px 30px rgba(0, 0, 0, 0.15);
        opacity: 0;
        transform: rotateX(-90deg) scale(0.8);
        transform-origin: center bottom;
        transition: all 1s cubic-bezier(0.68, -0.55, 0.27, 1.55);
        border-radius: 8px;
        overflow: hidden;
      }

      .divider {
        height: 1px;
        background: linear-gradient(
          90deg,
          rgba(198, 40, 40, 0) 0%,
          rgba(198, 40, 40, 0.4) 50%,
          rgba(198, 40, 40, 0) 100%
        );
        margin: 20px 0;
        opacity: 0;
        animation: dividerAppear 0.5s forwards;
      }

      .typing-line {
        overflow: hidden;
        white-space: nowrap;
        margin: 15px 0;
        position: relative;
        z-index: 1;
      }

      .typing-char {
        display: inline-block;
        opacity: 0;
        animation: charAppear 0.3s cubic-bezier(0.4, 0, 0.2, 1) forwards,
          textGlow 1.5s ease-in-out infinite;
      }
      .text {
        font-size: 24px;
        width: 80%;
        height: 400px;
        transform: translate(15%, 140%);
        /* 文字渐变色核心代码 */
        background: linear-gradient(to right, #ffdde1, #ee9ca7);
        -webkit-background-clip: text;
        background-clip: text;
        transition: all 0.5s;
      }
      .roll-in-left {
        animation: roll-in-left 0.6s ease-out both;
      }
      .slide-out-fwd-center {
        animation: slide-out-fwd-center 0.7s
          cubic-bezier(0.55, 0.085, 0.68, 0.53) both;
      }
      .start {
        /* 添加文字阴影提升可读性 */
        text-shadow: 0 2px 4px rgba(0, 0, 0, 0.5);
      }
      .end {
        /* 添加文字阴影提升可读性 */
        text-shadow: 0 5px 4px rgba(0, 0, 0, 1);
      }

      @keyframes charAppear {
        from {
          transform: translateY(20px);
        }
        to {
          opacity: 1;
          transform: translateY(0);
        }
      }

      @keyframes textGlow {
        50% {
          text-shadow: 0 0 8px #ffdde1, #ee9ca7;
        }
      }

      @keyframes dividerAppear {
        from {
          width: 0%;
        }
        to {
          width: 100%;
          opacity: 1;
        }
      }

      @keyframes roll-in-left {
        0% {
          transform: translateX(-800px) rotate(-540deg);
          opacity: 0;
        }
        100% {
          transform: translateX(0) rotate(0deg);
          opacity: 1;
        }
      }

      @keyframes slide-out-fwd-center {
        0% {
          transform: translateZ(1);
          opacity: 1;
        }
        100% {
          transform: translateZ(600px);
          opacity: 0;
        }
      }
      .envelope-container.open .envelope-icon {
        /* animation: none; */
        /* transform: rotateX(180deg) translateY(50px); */
        /* opacity: 0; */
        animation: slide-out-fwd-center 0.7s
          cubic-bezier(0.55, 0.085, 0.68, 0.53) both;
      }

      .envelope-container.open .letter-content {
        opacity: 1;
        transform: rotateX(0deg) scale(1);
      }
    </style>
  </head>
  <body>
    <div class="envelope-container">
      <div class="envelope-icon">
        <img
          class="roll-in-left"
          src="./img/letterCover.png"
          alt="信封"
          style="width: 3em; height: auto"
        />
      </div>
      <div class="letter-content">
        <div class="text" id="letterContent"></div>
      </div>
    </div>
    <script>
      const letterText = [
        "亲爱的妈妈：",
        "----------",
        "　　又到了每年的母亲节，过去的时间里我或许只能给一句问候",
        "----------",
        "但这句问候可能是现在的我能想到的最实用和真挚的礼物，",
        "----------",
        "这个特殊的节日里我想说——“您和爸爸在外多年，辛苦了！”",
        "----------",
        "　　母亲节快乐！愿岁月温柔待您，愿健康常伴您左右。",
        "　　　　　　　　　　　　　　　　　　　　　　永远爱您的儿子",
        "　　　　　　　　　　　　　　　　　　　　　　2025年5月11日",
      ];

      const container = document.querySelector(".envelope-container");
      const contentDiv = document.getElementById("letterContent");
      let isOpen = false;

      container.addEventListener("click", async () => {
        if (isOpen) return;
        isOpen = true;

        // 开始翻转动画
        container.classList.add("open");

        // 等待翻转动画完成
        await new Promise((r) => setTimeout(r, 800));

        // 显示信纸内容动画
        for (const line of letterText) {
          if (line.includes("-")) {
            const divider = document.createElement("div");
            divider.className = "divider";
            contentDiv.appendChild(divider);
            await new Promise((r) => setTimeout(r, 200));
          } else {
            await typeLine(line);
            await new Promise((r) => setTimeout(r, 150));
          }
        }
      });

      async function typeLine(text) {
        const lineDiv = document.createElement("div");
        lineDiv.className = "typing-line";
        contentDiv.appendChild(lineDiv);

        for (const char of text) {
          const charSpan = document.createElement("span");
          charSpan.className = "typing-char";
          charSpan.style.animationDelay = `${Math.random() * 0.1}s`;
          charSpan.textContent = char;
          lineDiv.appendChild(charSpan);
          await new Promise((r) => setTimeout(r, char === "　" ? 10 : 50));
        }
      }
    </script>
  </body>
</html>
